#!/bin/bash

echo "╔════════════════════════════════════════════════════════════╗"
echo "║       SFTPX - NPM 发布前检查                              ║"
echo "╚════════════════════════════════════════════════════════════╝"
echo ""

PASS=0
FAIL=0

check() {
  if [ $? -eq 0 ]; then
    echo "✅ $1"
    ((PASS++))
  else
    echo "❌ $1"
    ((FAIL++))
  fi
}

# 1. 检查 package.json
echo "📋 检查 package.json..."
if [ -f "package.json" ]; then
  if grep -q '"name"' package.json && grep -q '"version"' package.json; then
    echo "✅ package.json 格式正确"
    ((PASS++))
  else
    echo "❌ package.json 缺少必要字段"
    ((FAIL++))
  fi
else
  echo "❌ package.json 文件不存在"
  ((FAIL++))
fi

# 2. 检查源文件
echo ""
echo "📂 检查源文件..."
files=("src/index.js" "src/cli.js" "src/config/configManager.js" "src/sftp/sftpClient.js" "src/sftp/batchTransfer.js")
for file in "${files[@]}"; do
  if [ -f "$file" ]; then
    node --check "$file" 2>/dev/null
    check "$file"
  else
    echo "❌ $file 不存在"
    ((FAIL++))
  fi
done

# 3. 检查文档
echo ""
echo "📚 检查文档..."
docs=("README.md" "LICENSE")
for doc in "${docs[@]}"; do
  if [ -f "$doc" ]; then
    echo "✅ $doc"
    ((PASS++))
  else
    echo "⚠️  $doc 不存在"
  fi
done

# 4. 检查 npm 依赖
echo ""
echo "📦 检查 npm 依赖..."
if [ -d "node_modules" ]; then
  echo "✅ npm 依赖已安装"
  ((PASS++))
else
  echo "❌ npm 依赖未安装"
  ((FAIL++))
fi

# 5. 检查 bin 配置
echo ""
echo "🔧 检查 bin 配置..."
if grep -q '"sftp-cli"' package.json && grep -q '"sftpx"' package.json; then
  echo "✅ bin 配置正确"
  ((PASS++))
else
  echo "❌ bin 配置不完整"
  ((FAIL++))
fi

# 6. 检查 npm 登录状态
echo ""
echo "🔐 检查 npm 登录..."
if npm whoami > /dev/null 2>&1; then
  echo "✅ 已登录 npm: $(npm whoami)"
  ((PASS++))
else
  echo "⚠️  未登录 npm (发布前需要登录)"
fi

# 7. 显示包大小
echo ""
echo "📊 计算包大小..."
npm pack --dry-run 2>/dev/null | grep -A1 "contents:"

# 8. 验证命令
echo ""
echo "🚀 验证命令..."
if npm link > /dev/null 2>&1; then
  if which sftp-cli > /dev/null 2>&1 && which sftpx > /dev/null 2>&1; then
    echo "✅ 命令已安装"
    ((PASS++))
    npm unlink > /dev/null 2>&1
  else
    echo "❌ 命令安装失败"
    ((FAIL++))
  fi
else
  echo "⚠️  无法测试本地安装"
fi

# 显示总结
echo ""
echo "╔════════════════════════════════════════════════════════════╗"
echo "║                    检查结果                                ║"
echo "╠════════════════════════════════════════════════════════════╣"
echo "║ 通过: ✅ $PASS                                             ║"
echo "║ 失败: ❌ $FAIL                                             ║"

if [ $FAIL -eq 0 ]; then
  echo "║                                                            ║"
  echo "║ 🎉 所有检查通过！准备发布 npm 包                         ║"
  echo "║                                                            ║"
  echo "║ 后续步骤：                                               ║"
  echo "║   1. npm login                (登录 npm)                 ║"
  echo "║   2. npm version patch        (更新版本)                 ║"
  echo "║   3. npm publish              (发布包)                   ║"
  echo "╚════════════════════════════════════════════════════════════╝"
  exit 0
else
  echo "║                                                            ║"
  echo "║ ⚠️  发现 $FAIL 个问题，请修复后再发布                    ║"
  echo "║                                                            ║"
  echo "║ 详见：README.md                                           ║"
  echo "╚════════════════════════════════════════════════════════════╝"
  exit 1
fi
